package com.itheima.tanhua.web;

import com.itheima.tanhua.exception.ConsumertException;
import com.itheima.tanhua.service.UserInfoService;
import com.itheima.tanhuan.dto.db.UserInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 保存用户详细信息
     * @param token
     * @param userInfoDto
     * @return
     */
    @PostMapping("loginReginfo")
    public ResponseEntity loginReginfo(@RequestHeader("Authorization") String token,
                                       @RequestBody UserInfoDto userInfoDto){
        userInfoService.loginReginfo(token,userInfoDto);
        return ResponseEntity.ok(null);
    }

    /**
     * 上传头像：修改用户信息中avatar数据
     * @param token
     * @param headPhoto
     * @return
     */
    @PostMapping("loginReginfo/head")
    public ResponseEntity head(@RequestHeader("Authorization") String token, MultipartFile headPhoto){
        Integer flag = userInfoService.uploadHead(token, headPhoto);
        if (flag==1){
            throw new ConsumertException("上传的非人像");
        }
        if (flag==2){
            throw new ConsumertException("头像上传失败！");
        }
        return ResponseEntity.ok(null);
    }
}
